package com.hoopchina.util.xml;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.hoopchina.meta.NewsItem;
import com.hoopchina.meta.NewsItemDetail;
import com.hoopchina.meta.NewsSearchResultItem;
import com.hoopchina.meta.NewsSearchResultList;


import android.util.Log;
import android.util.Xml;

public class NewsDetailXML {
    private static final String XMLPULL_INFO = "XmlPull_Info";

    public NewsItemDetail getNewsList(InputStream ainputstream) {
    	NewsItemDetail mCurrentNewsItem = null;
        //InputStream in =null;
        XmlPullParser mXmlPull = Xml.newPullParser();
      
//        将GBK转为utf-8       
//            BufferedReader bf;
//			try {
//				bf = new BufferedReader(new InputStreamReader(ainputstream,"GBK"));
//		           StringBuffer buffer=new StringBuffer();  
//		            String line="";  
//		            while((line=bf.readLine())!=null){  
//		                buffer.append(line);  
//		            }  
//		              
//		        String buff = buffer.toString();  
//		        in=new ByteArrayInputStream(buff.getBytes());  
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}  
            //最好在将字节流转换为字符流的时候 进行转码  

 

	// inputStream=this.context.getResources().getAssets().open("student.xml");
	try {

	    /**
	     * 提供的xml内容方式都是以输入流的方式InputStream
	     **/
	    mXmlPull.setInput( ainputstream, "UTF-8");
	    /**
	     * 触发事件getEventType() =>返回事件码 当它遇到某个字符,如果符合xml的语法规范. 它就会出发这个语法所代表的数字
	     * Returns the type of the current event (START_TAG, END_TAG, TEXT,
	     * etc.)
	     **/
	    int eventCode = mXmlPull.getEventType();

	    /**
	     * 解析事件: StartDocument,文档开始 Enddocument,文档结束
	     * 
	     * 每次读到一个字符,就产生一个事件 只要解析xml文档事件不为空,就一直往下读
	     **/
	    while (eventCode != XmlPullParser.END_DOCUMENT) {
           

    		switch (eventCode) {
    		case XmlPullParser.START_DOCUMENT: // 文档开始事件,可以做一些数据初始化处理
    			//mNewsResultList =  new ArrayList<NewsItem>();
    		    break;
    
    		case XmlPullParser.START_TAG:// 元素开始.
    			String  name = mXmlPull.getName();
                if (name.equalsIgnoreCase("news")) {
    		        mCurrentNewsItem = new NewsItemDetail();
    		    } 
                else  if (mCurrentNewsItem != null){
                if (name.equalsIgnoreCase("title")) {
    		        mCurrentNewsItem.setTitle( mXmlPull.nextText() );
    		    }
                else if (name.equalsIgnoreCase("c")) {
    		    	int m = mXmlPull.getAttributeCount();
   // 		    	for(int i=0; i<m;i++){
    		    		String index =mXmlPull.getAttributeValue(null, "index");
    		    		String type = mXmlPull.getAttributeValue(null, "type");
   		    		    String subcontent = mXmlPull.nextText();
 //   		    	}
    		    		mCurrentNewsItem.addSubContent(index, type, subcontent);

                } else if (name.equalsIgnoreCase("imgUrl")) {
                    mCurrentNewsItem.setImgUrl( mXmlPull.nextText() );
                } else if (name.equalsIgnoreCase("logoimge")) {
                    mCurrentNewsItem.setLogoimg( mXmlPull.nextText() );
                } else if (name.equalsIgnoreCase("url")) {
                    mCurrentNewsItem.setUrl( mXmlPull.nextText() );
                }  else if (name.equalsIgnoreCase("time")) {
                    mCurrentNewsItem.setTime( mXmlPull.nextText() );
                } else if (name.equalsIgnoreCase("site")) {
                    mCurrentNewsItem.setSource( mXmlPull.nextText() );
                } else { 
                    // throw
                }
                }
    		    break;
    
    		case XmlPullParser.END_TAG: // 元素结束,
    
    		    break;
    		default :
    		    break;
    		}
		eventCode = mXmlPull.next();// 进入到一下一个元素. 
	    }
	} catch (XmlPullParserException e) {
	    Log.i(XMLPULL_INFO, e.toString());
	} catch (IOException e) {
	    Log.i(XMLPULL_INFO, e.toString());
	} catch (Exception e) {
	    Log.i(XMLPULL_INFO, e.toString());
	}
	return mCurrentNewsItem;
    }

}